package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"sqmail/maild"
	"strings"
)

var debug = debugT(true)

type debugT bool

func (d debugT) Printf(format string, args ...interface{}) {
	if d {
		log.Printf(format, args...)
	}
}

func (d debugT) Println(args ...interface{}) {
	if d {
		log.Println(args)
	}
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	fmt.Print("username: ")
	username_, _ := reader.ReadBytes('\n')
	fmt.Print("password: ")
	password_, _ := reader.ReadBytes('\n')
	username := strings.TrimRight(string(username_), "\t\r\n")
	password := strings.TrimRight(string(password_), "\t\r\n")

	mailServer, err := maild.NewMail("pop3.163.com", "995")
	if err != nil {
		debug.Println(err)
		return
	}
	err = mailServer.SetAuth(username, password)
	if err != nil {
		debug.Println(err)
		return
	}

	for {
		fmt.Print("> ")
		in, _ := reader.ReadBytes('\n')

		line := strings.TrimRight(string(in), "\t\r\n")
		opts := strings.Split(line, " ")
		cmd := opts[0]

		if cmd == "exit" || cmd == "quit" {
			fmt.Println("bye")
			return
		}

		err = mailServer.Cmd(cmd, opts[1:])
		if err != nil {
			debug.Println(err)
			return
		}
	}
}
